library(tidyverse)
library(ggthemes)## Warning: package 'ggthemes' was built under R version 3.4.4
library(knitr)
library(broom)
library(stringr)
library(lubridate)
options(digits = 3)
set.seed(1234)
theme_set(theme_minimal())theme_void2 <- function (base_size = 11, base_family = "", base_line_size = base_size/22,
base_rect_size = base_size/22) {
half_line <- base_size/2
theme(line = element_blank(),
rect = element_blank(),
text = element_text(family = base_family,
face = "plain",
colour = "black",
size = base_size,
lineheight = 0.9,
hjust = 0.5,
vjust = 0.5,
angle = 0,
margin = margin(), debug = FALSE),
axis.text.y = element_blank(),
axis.title = element_blank(),
axis.ticks.length = unit(0, "pt"),
legend.key.size = unit(1.2, "lines"),
legend.position = "right",
legend.text = element_text(size = rel(0.8)),
legend.title = element_text(hjust = 0),
strip.text = element_text(size = rel(0.8)),
strip.switch.pad.grid = unit(0.1, "cm"),
strip.switch.pad.wrap = unit(0.1, "cm"),
panel.ontop = FALSE,
panel.spacing = unit(half_line, "pt"),
plot.margin = unit(c(0, 0, 0, 0), "lines"),
plot.title = element_text(size = rel(1.2), hjust = 0,
vjust = 1, margin = margin(t = half_line * 1.2)),
plot.subtitle = element_text(size = rel(0.9), hjust = 0,
vjust = 1, margin = margin(t = half_line * 0.9)),
plot.caption = element_text(size = rel(0.9), hjust = 1,
vjust = 1, margin = margin(t = half_line * 0.9)),
complete = TRUE)
}A graphical form that involves elementary perceptual tasks that lead to more accurate judgments than another graphical form (with the same quantitiative information) will result in better organization and increase the chances of a correct perception of patterns and behavior.
Figure 6.12 from The Functional Art
diamonds_sum <- diamonds %>%
group_by(cut) %>%
summarize(n = n()) %>%
ungroup %>%
mutate(pct = n / sum(n))
ggplot(diamonds_sum, aes(cut, pct)) +
geom_col() +
scale_y_continuous(labels = scales::percent) +
labs(title = "Diamonds data",
x = "Cut of diamond",
y = "Percentage of sample")ggplot(diamonds_sum, aes(x = factor(1), y = pct, fill = cut)) +
geom_col(width = 1) +
coord_polar(theta = "y", direction = -1) +
theme_void()library(fiftystater)
data("fifty_states") # this line is optional due to lazy data loading
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
# map_id creates the aesthetic mapping to the state name column in your data
p <- ggplot(crimes, aes(map_id = state)) +
# map points to the fifty_states shape data
geom_map(aes(fill = Assault), map = fifty_states) +
expand_limits(x = fifty_states$long, y = fifty_states$lat) +
coord_map() +
scale_x_continuous(breaks = NULL) +
scale_y_continuous(breaks = NULL) +
labs(title = "Assaults per 100,000 residents in 1973",
x = NULL,
y = NULL) +
theme(legend.position = "bottom",
panel.background = element_blank()) +
fifty_states_inset_boxes()
# default colors
plibrary(statebins)
USArrests %>%
rownames_to_column("State") %>%
statebins(state_col = "State",
value_col = "Assault") +
labs(title = "Assaults per 100,000 residents in 1973",
x = NULL,
y = NULL) +
theme_statebins()# generate correlated data
xy <- ecodist::corgen(len = 20, r = .95) %>%
bind_cols
ggplot(xy, aes(x, y)) +
geom_point()xy %>%
mutate(id = row_number()) %>%
gather(var, value, -id) %>%
ggplot(aes(id, value, fill = var)) +
geom_col(position = "dodge")polls <- read_csv("https://projects.fivethirtyeight.com/trump-approval-data/approval_polllist.csv")## Parsed with column specification:
## cols(
## .default = col_character(),
## samplesize = col_integer(),
## weight = col_double(),
## influence = col_integer(),
## approve = col_double(),
## disapprove = col_double(),
## adjusted_approve = col_double(),
## adjusted_disapprove = col_double(),
## tracking = col_logical(),
## poll_id = col_integer(),
## question_id = col_integer()
## )
## See spec(...) for full column specifications.
## Warning in rbind(names(probs), probs_f): number of columns of result is not
## a multiple of vector length (arg 1)
## Warning: 210 parsing failures.
## row # A tibble: 5 x 5 col row col expected actual file expected <int> <chr> <chr> <chr> <chr> actual 1 1546 influence an integer .0013319 'https://projects.fivethirtyeight.c… file 2 1547 influence an integer .0056207 'https://projects.fivethirtyeight.c… row 3 1548 influence an integer .0051985 'https://projects.fivethirtyeight.c… col 4 1549 influence an integer .0009761 'https://projects.fivethirtyeight.c… expected 5 1550 influence an integer .0025643 'https://projects.fivethirtyeight.c…
## ... ................. ... .......................................................................... ........ .......................................................................... ...... .......................................................................... .... .......................................................................... ... .......................................................................... ... .......................................................................... ........ ..........................................................................
## See problems(...) for more details.
polls_data <- polls %>%
filter(subgroup == "All polls") %>%
select(enddate, adjusted_approve, adjusted_disapprove) %>%
mutate(date = mdy(enddate))
polls_data %>%
gather(var, value, starts_with("adjusted")) %>%
mutate(var = factor(var, levels = c("adjusted_approve", "adjusted_disapprove"),
labels = c("Approve", "Disapprove")),
value = value / 100) %>%
ggplot(aes(date, value, color = var)) +
geom_smooth() +
scale_y_continuous(label = scales::percent) +
labs(title = "How popular/unpopular is Donald Trump?",
x = NULL,
y = NULL,
color = NULL,
caption = "Source: FiveThirtyEight") +
theme(legend.position = "bottom")## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
polls_data %>%
mutate(diff = (adjusted_approve - adjusted_disapprove) / 100) %>%
ggplot(aes(date, diff)) +
geom_hline(yintercept = 0) +
geom_smooth() +
scale_y_continuous(label = scales::percent) +
labs(title = "How popular/unpopular is Donald Trump?",
x = NULL,
y = "Net approval rating",
caption = "Source: FiveThirtyEight")## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
A screenshot of data. Source: The Functional Art
A proportional symbol map. Source: The Functional Art
Chloropeth map. Source: The Functional Art
Dot chart. Source: The Functional Art
Scatterplot. Source: The Functional Art
Slopegraph. Source: The Functional Art
ggplot(mpg, aes(cty, hwy)) +
geom_point()ggplot(mpg, aes(cty, hwy)) +
geom_point() +
theme(panel.grid.major = element_line("grey70"),
panel.grid.minor = element_line("grey70"))ggplot(mpg, aes(cty, hwy)) +
geom_point() +
theme(panel.grid.major = element_line("grey50"),
panel.grid.minor = element_line("grey50"))ggplot(mpg, aes(cty, hwy)) +
geom_point() +
theme(panel.grid.major = element_line("grey20"),
panel.grid.minor = element_line("grey20"))Psychophysical theory of perception. Function of relationship between perceived areas of circles and their physical areas
\[\text{Subjective area} = \text{Area}^.86\]
pie <- data_frame(label = c("A", "B", "C", "D"),
value = c(10, 20, 40, 30))
# bar chart
ggplot(pie, aes(label, value)) +
geom_col(color = "black", fill = "white") +
theme_void2()# proportional area chart
pie_prop <- pie %>%
mutate(ymin = 0,
ymax = 1,
xmax = cumsum(value),
xmin = xmax - value,
x = (xmax - xmin) / 2 + xmin)
ggplot(pie_prop, aes(xmin = xmin, xmax = xmax,
ymin = ymin, ymax = ymax)) +
geom_rect(fill = "white", color = "black") +
scale_x_continuous(breaks = pie_prop$x, labels = pie_prop$label) +
theme_void2()# pie chart
ggplot(pie, aes(x = factor(1), y = value)) +
geom_col(width = 1, color = "black", fill = "white") +
geom_text(aes(label = label), position = position_stack(vjust = .5), size = 4) +
coord_polar(theta = "y", direction = 1) +
theme_void() +
theme(legend.position = "none")# table
pie %>%
select(value) %>%
t %>%
knitr::kable(col.names = pie$label,
row.names = FALSE)| A | B | C | D |
|---|---|---|---|
| 10 | 20 | 40 | 30 |
devtools::session_info()## Session info -------------------------------------------------------------
## setting value
## version R version 3.4.3 (2017-11-30)
## system x86_64, darwin15.6.0
## ui X11
## language (EN)
## collate en_US.UTF-8
## tz America/Chicago
## date 2018-04-17
## Packages -----------------------------------------------------------------
## package * version date source
## assertthat 0.2.0 2017-04-11 CRAN (R 3.4.0)
## backports 1.1.2 2017-12-13 CRAN (R 3.4.3)
## base * 3.4.3 2017-12-07 local
## bindr 0.1.1 2018-03-13 CRAN (R 3.4.3)
## bindrcpp 0.2.2.9000 2018-04-08 Github (krlmlr/bindrcpp@bd5ae73)
## broom * 0.4.4 2018-03-29 CRAN (R 3.4.3)
## cellranger 1.1.0 2016-07-27 CRAN (R 3.4.0)
## cli 1.0.0 2017-11-05 CRAN (R 3.4.2)
## colorspace 1.3-2 2016-12-14 CRAN (R 3.4.0)
## compiler 3.4.3 2017-12-07 local
## crayon 1.3.4 2017-10-03 Github (gaborcsardi/crayon@b5221ab)
## datasets * 3.4.3 2017-12-07 local
## devtools 1.13.5 2018-02-18 CRAN (R 3.4.3)
## digest 0.6.15 2018-01-28 CRAN (R 3.4.3)
## dplyr * 0.7.4.9003 2018-04-08 Github (tidyverse/dplyr@b7aaa95)
## evaluate 0.10.1 2017-06-24 CRAN (R 3.4.1)
## forcats * 0.3.0 2018-02-19 CRAN (R 3.4.3)
## foreign 0.8-69 2017-06-22 CRAN (R 3.4.3)
## ggplot2 * 2.2.1.9000 2018-04-10 Github (tidyverse/ggplot2@3c9c504)
## ggthemes * 3.4.2 2018-04-03 CRAN (R 3.4.4)
## glue 1.2.0 2017-10-29 CRAN (R 3.4.2)
## graphics * 3.4.3 2017-12-07 local
## grDevices * 3.4.3 2017-12-07 local
## grid 3.4.3 2017-12-07 local
## gtable 0.2.0 2016-02-26 CRAN (R 3.4.0)
## haven 1.1.1 2018-01-18 CRAN (R 3.4.3)
## hms 0.4.2 2018-03-10 CRAN (R 3.4.3)
## htmltools 0.3.6 2017-04-28 CRAN (R 3.4.0)
## httr 1.3.1 2017-08-20 CRAN (R 3.4.1)
## jsonlite 1.5 2017-06-01 CRAN (R 3.4.0)
## knitr * 1.20 2018-02-20 CRAN (R 3.4.3)
## lattice 0.20-35 2017-03-25 CRAN (R 3.4.3)
## lazyeval 0.2.1 2017-10-29 CRAN (R 3.4.2)
## lubridate * 1.7.4 2018-04-11 CRAN (R 3.4.3)
## magrittr 1.5 2014-11-22 CRAN (R 3.4.0)
## memoise 1.1.0 2017-04-21 CRAN (R 3.4.0)
## methods * 3.4.3 2017-12-07 local
## mnormt 1.5-5 2016-10-15 CRAN (R 3.4.0)
## modelr 0.1.1 2017-08-10 local
## munsell 0.4.3 2016-02-13 CRAN (R 3.4.0)
## nlme 3.1-137 2018-04-07 CRAN (R 3.4.4)
## parallel 3.4.3 2017-12-07 local
## pillar 1.2.1 2018-02-27 CRAN (R 3.4.3)
## pkgconfig 2.0.1 2017-03-21 CRAN (R 3.4.0)
## plyr 1.8.4 2016-06-08 CRAN (R 3.4.0)
## psych 1.8.3.3 2018-03-30 CRAN (R 3.4.4)
## purrr * 0.2.4 2017-10-18 CRAN (R 3.4.2)
## R6 2.2.2 2017-06-17 CRAN (R 3.4.0)
## Rcpp 0.12.16 2018-03-13 CRAN (R 3.4.4)
## readr * 1.1.1 2017-05-16 CRAN (R 3.4.0)
## readxl 1.0.0 2017-04-18 CRAN (R 3.4.0)
## reshape2 1.4.3 2017-12-11 CRAN (R 3.4.3)
## rlang 0.2.0.9001 2018-04-10 Github (r-lib/rlang@70d2d40)
## rmarkdown 1.9 2018-03-01 CRAN (R 3.4.3)
## rprojroot 1.3-2 2018-01-03 CRAN (R 3.4.3)
## rstudioapi 0.7 2017-09-07 CRAN (R 3.4.1)
## rvest 0.3.2 2016-06-17 CRAN (R 3.4.0)
## scales 0.5.0.9000 2018-04-10 Github (hadley/scales@d767915)
## stats * 3.4.3 2017-12-07 local
## stringi 1.1.7 2018-03-12 CRAN (R 3.4.3)
## stringr * 1.3.0 2018-02-19 CRAN (R 3.4.3)
## tibble * 1.4.2 2018-01-22 CRAN (R 3.4.3)
## tidyr * 0.8.0 2018-01-29 CRAN (R 3.4.3)
## tidyselect 0.2.4 2018-02-26 CRAN (R 3.4.3)
## tidyverse * 1.2.1 2017-11-14 CRAN (R 3.4.2)
## tools 3.4.3 2017-12-07 local
## utils * 3.4.3 2017-12-07 local
## withr 2.1.2 2018-04-10 Github (jimhester/withr@79d7b0d)
## xml2 1.2.0 2018-01-24 CRAN (R 3.4.3)
## yaml 2.1.18 2018-03-08 CRAN (R 3.4.4)